<template>
    <div class="app-container">
        <el-row :gutter="20">
            <el-col :span="24" :xs="24">
                <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
                    <el-form-item :label="$locale('myDevice.undetermined.Name of turnover box')" prop="boxName">
                        <el-input v-model="queryParams.boxName" :placeholder="$locale('myDevice.undetermined.Please enter the name of the turnover box')" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item :label="$locale('myDevice.undetermined.Turnover box number')" prop="boxNum">
                        <el-input v-model="queryParams.boxNum" :placeholder="$locale('myDevice.undetermined.Please enter the turnover box number')" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item :label="$locale('geWudevice.Equipment number')" prop="deviceNum">
                        <el-input v-model="queryParams.deviceNum" :placeholder="$locale('myDevice.determinedList.Please enter the device number')" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item :label="$locale('geWudevice.Equipment name')" prop="deviceName">
                        <el-input v-model="queryParams.deviceName" :placeholder="$locale('geWudevice.Please enter the device name')" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{ $locale('geWudevice.add.search for') }}</el-button>
                        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $locale('geWudevice.reset') }}</el-button>
                    </el-form-item>
                </el-form>

                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">{{ $locale('classList.add.added') }}</el-button>
                    </el-col>
                </el-row>

                <el-table v-loading="loading" :data="listData" row-key="id" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55"> </el-table-column>
                    <el-table-column :label="$locale('myDevice.determinedList.device')" align="center" key="boxName" prop="boxName">
                        <template slot-scope="scope">
                            {{ scope.row.boxName }}
                        </template>
                    </el-table-column>
                    <el-table-column :label="$locale('myDevice.determinedList.Equipment code')" align="center" key="boxNum" prop="boxNum" width="160">
                    </el-table-column>
                    <el-table-column :label="$locale('geWudevice.quantity')" align="center" key="deviceTotal" prop="deviceTotal">
                    </el-table-column>
                    <el-table-column :label="$locale('classList.add.notes')" align="center" key="remark" prop="remark">
                    </el-table-column>
                    <el-table-column :label="$locale('formMarkering.list.operate')" align="center" width="160" class-name="small-padding fixed-width">
                        <template slot-scope="scope" v-if="scope.row.hasChildren === false">
                            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">{{ $locale('classList.add.modify') }}</el-button>
                            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">
                              {{ $locale('formMarkering.list.delete') }}</el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
            </el-col>
        </el-row>

        <!-- 添加 -->
        <addTurnoverBox ref="addTurnoverBox" @getList="getList"></addTurnoverBox>
    </div>
</template>

<script>
import { deleteById, list } from '@/api/device/turnoverBoxList'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import addTurnoverBox from './add/index.vue'
import { getNames } from '@/utils/index'

export default {
    name: 'experiment',
    components: { Treeselect, addTurnoverBox },
    data () {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 用户表格数据
            listData: [],
            // 弹出层标题
            title: '',
            // 部门树选项
            deptOptions: undefined,
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                boxName: '',
                boxNum: '',
                deviceNum: '',
                deviceName: '',
            },
        }
    },
    created () {
        this.getList()
    },
    methods: {
        /** 查询用户列表 */
        getList () {
            this.loading = true
            list(this.queryParams).then(({ data }) => {
                this.listData = data.records.map((item, index) => {
                    item['hasChildren'] = false
                    item.children = item.deviceTurnoverBoxModelVOS.map((i, t) => {
                        i.id = index + t.toString() + 'child' + i.deviceId
                        i.boxName = i.deviceName
                        i.boxNum = i.deviceNum
                        return i
                    })
                    return item
                })
                this.total = data.total
                this.loading = false
            })
        },
        /** 搜索按钮操作 */
        handleQuery () {
            this.queryParams.pageNum = 1
            this.getList()
        },
        handleSelectionChange (selection) {
            this.ids = selection.map((item) => item.id)
        },
        /** 重置按钮操作 */
        resetQuery () {
            this.resetForm('queryForm')
            this.handleQuery()
        },
        /** 新增按钮操作 */
        handleAdd () {
            this.$refs.addTurnoverBox.handleAdd()
        },
        /** 修改按钮操作 */
        handleUpdate (row) {
            row.id = row.id ? this.replaceStr(row.id) : ''
            this.$refs.addTurnoverBox.handleUpdate(row)
        },
        replaceStr (str, search = 'child') {
            str = str.toString()
            const index = str.indexOf(search)
            if (index !== -1) {
                return str.substring(index + search.length)
            }
            return str
        },
        /** 删除按钮操作 */
        handleDelete (row) {
            row.id = row.id ? this.replaceStr(row.id) : ''
            const ids =
                row.id ||
                this.ids.map((item) => {
                    item = item ? this.replaceStr(item) : ''
                    return item
                })
            this.$modal
                .confirm(
                    '是否确认删除名称为"' +
                    getNames(this.listData, ids, 'boxName') +
                    '"的数据项？',
                )
                .then(function () {
                    return deleteById({ id: ids })
                })
                .then(() => {
                    this.getList()
                    this.$modal.msgSuccess('删除成功')
                })
                .catch(() => { })
        },
    },
}
</script>
<style lang="scss" scoped>
::v-deep .el-table__row--level-1 .el-checkbox {
    display: none;
}
</style>
